package com.ling.team.service;

import com.ling.team.domain.*;

public class TeamService {
    private final int MAX_MEMBER = 5;
    private Programmer[] team = new Programmer[MAX_MEMBER];
    private int count = 0;
    private int total;

    public int getCount(){
        return count;
    }
    public Programmer[] getTeam(){
        return team;
    }
    public String addMember(Employee employee){
        if (count == MAX_MEMBER){
            System.out.println("成员已满，无法添加");
            return "成员已满，无法添加";
        }
        //该成员不是开发成员，无法添加
        if (!(employee instanceof Programmer)){
            System.out.println("该成员不是开发成员，无法添加");
            return "该成员不是开发成员，无法添加";
        }
        Programmer programmer = (Programmer) employee;

        //该员工已在本开发团队中
        for (int i =0;i<count;i++){
            if (programmer.getId() == team[i].getId()){
                System.out.println("该员工已在本开发团队中");
                return "该员工已在本开发团队中";
            }
        }

        //团队中至多只能有一名架构师
        //团队中至多只能有两名设计师
        //团队中至多只能有三名程序员
        int architectCount = 0;
        int designerCount = 0;
        int programmerCount = 0;

        for (int i = 0;i<count;i++){
            if (team[count] instanceof Architect){
                architectCount++;
            }else if(team[count] instanceof Designer){
                designerCount++;
            }else{
                programmerCount++;
            }
        }
        //注意判断顺序，先判断 Architect
        if (programmer instanceof Architect){
            if (architectCount >= 1){
                System.out.println("团队中至多只能有一名架构师");
                return "团队中至多只能有一名架构师";
            }
        }else if(programmer instanceof Designer){
            if (designerCount >= 2){
                System.out.println("团队中至多只能有两名设计师");
                return "团队中至多只能有两名设计师";
            }
        }else{
            if (programmerCount >= 3){
                System.out.println("团队中至多只能有三名程序员");
                return "团队中至多只能有三名程序员";
            }
        }
        programmer.setStatus(Status.Busy);

        team[count] = programmer;
        count++;

        programmer.setMemberId(count);

        return "";
    }

    public String removeMember(int memberId){
        int findIndex = -1;
        for (int i = 0;i < count;i++){
            if (memberId == team[i].getMemberId()){
                findIndex = i;
            }
        }

        if (findIndex == -1) {
            return "删除的员工不存在团队中";
        }
        for (int i = findIndex; i > count - 1; i++) {
            team[i] = team[i+1];
        }
        team[count - 1] = null;

        count--;
        return "";
    }


    public void setTotal(int total) {
        this.total = total;
    }

    public int getTotal(int total) {
        return total;
    }
}
